<?php 
defined('RUN_KATANA') or die('Restricted access');
class Site extends Object
{
	public $Title;
	public $URL;
	public $Language;
	public $License;
	public $MetaTitle;
	public $MetaData;
	public $MetaKey;
	public $AnalyticsID;
	public $Active;
	
	public $CompanyName;
	public $CompanyNumber;
	public $CompanyAddress;
	public $CompanyPhone;
	public $CompanyEmail;
	public $ValuesLoaded;
	
	public function Site($Id) 
	{ 
		$this->ValuesLoaded = false;
		self::setId($Id);
		self::loadValues();
	}
	
	/* GET FUNCTIONS */
	public function getActive() { return intval($this->Active); }
	/* SET FUNCTIONS */
	public function setActive($val) { $this->Active=intval($val); }
	
	public function loadValues()
	{
		if(($this->ValuesLoaded == false) && ($this->Id > 0))
		{
			global $db;
			$sql  = 'SELECT * FROM '.DB_PERFIX.'sites WHERE id='.$this->Id.';';
			
			$result = $db->query($sql);
			$data = $db->fetch($sql, CACHE_TTL);
				
			if(is_array($data))
			{
				foreach($data as $row)
				{
					self::setId($row['id']);
					self::setTitle($row['title']);
					self::setURL($row['site_url']);
					self::setLicense($row['license']);
					self::setLanguage($row['language']);
					self::setMetaTitle($row['MetaTitle']);
					self::setMetaData($row['meta_data']);
					self::setMetaKey($row['meta_key']);
					self::setAnalyticsID($row['analytics_id']);
					self::setCompanyName($row['company_name']);
					self::setCompanyNumber($row['company_number']);
	 				self::setCompanyAddress($row['company_address']);
					self::setCompanyPhone($row['company_phone']);
					self::setCompanyEmail($row['company_email']);
					self::setActive($row['active']);
					$this->ValuesLoaded = true;
				}
			}
		}
	}
	
	public function update()
	{
		if((self::getId() > 0) && ($this->ValuesLoaded))
		{
			self::formatMetaKey();
			$sql  = 'UPDATE '.DB_PERFIX.'sites SET ';
			$sql .= '`title`=\''.mysql_real_escape_string(self::getTitle()).'\', ';
			$sql .= '`site_url`=\''.mysql_real_escape_string(self::getURL()).'\', ';
			$sql .= '`license`=\''.mysql_real_escape_string(self::getLicense()).'\', ';
			$sql .= '`language`=\''.mysql_real_escape_string(self::getLanguage()).'\', ';
			$sql .= '`MetaTitle`=\''.mysql_real_escape_string(self::getMetaTitle()).'\', ';
			$sql .= '`meta_data`=\''.mysql_real_escape_string(self::getMetaData()).'\', ';
			$sql .= '`meta_key`=\''.mysql_real_escape_string(self::getMetaKey()).'\', ';
			$sql .= '`analytics_id`=\''.mysql_real_escape_string(self::getAnalyticsID()).'\', ';
			$sql .= '`company_name`=\''.mysql_real_escape_string(self::getCompanyName()).'\', ';
			$sql .= '`company_number`=\''.mysql_real_escape_string(self::getCompanyNumber()).'\', ';
			$sql .= '`company_address`=\''.mysql_real_escape_string(self::getCompanyAddress()).'\', ';
			$sql .= '`company_phone`=\''.mysql_real_escape_string(self::getCompanyPhone()).'\', ';
			$sql .= '`company_email`=\''.mysql_real_escape_string(self::getCompanyEmail()).'\', ';
			$sql .= '`active`='.self::getActive().' ';
			$sql .= 'WHERE `id`='.self::getId().';';
			//echo $sql;
			$result = mysql_query($sql);
		}
	}
	
	public function formatMetaKey()
	{
		if($this->MetaKey)
		{
			$this->MetaKey = strtolower($this->MetaKey);
			$this->MetaKey = str_replace(' ',', ',$this->MetaKey);
			$this->MetaKey = str_replace(',,',', ',$this->MetaKey);
			$this->MetaKey = str_replace(', ,',', ',$this->MetaKey);
			$this->MetaKey = str_replace(' ,',',',$this->MetaKey);
			$this->MetaKey = str_replace(', , ',', ',$this->MetaKey);
			$this->MetaKey = str_replace('  ',' ',$this->MetaKey);
		}
	}
}
?>